package com.css.fxfzaqbz.modules.data.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.ExpertManagement;
import com.css.fxfzaqbz.modules.data.repository.ExpertManagementRepository;
import com.css.fxfzaqbz.modules.data.service.ExpertManagementService;
import com.css.fxfzaqbz.util.CreateGson;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ExpertManagementServiceImpl implements ExpertManagementService {
    @Resource
    private ExpertManagementRepository expertManagementRepository;
    @Resource
    private SUserService sUserService;

    @Override
    public void save(List<String> ids) {
        List<ExpertManagement> expertManagements = new ArrayList<>();
        for (String id : ids) {
            ExpertManagement expertManagement = new ExpertManagement();
            String userId = PlatformSessionUtils.getUserId();
            String userJson = sUserService.getSUser(userId);
            SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
            expertManagement.setId(UUIDGenerator.getUUID());
            expertManagement.setDelFlag(YNEnum.N.toString());
            expertManagement.setCreateUser(userId);
            expertManagement.setProvinceId(sUser.getProvinceId());
            expertManagement.setCreateTime(PlatformDateUtils.getCurrentTimestamp());

            expertManagement.setExpertId(id);
            String userJson1 = sUserService.getSUser(id);
            SUser expert = CreateGson.createGson().fromJson(userJson1, SUser.class);
            expertManagement.setExpertName(expert.getUserName());
            expertManagement.setPhone(expert.getMobilePhone());
            expertManagement.setDept(expert.getExtend5());
            expertManagements.add(expertManagement);
        }
        expertManagementRepository.save(expertManagements);
    }

    @Override
    public Map<String, Object> getAll(int curPage, int pageSize, ExpertManagement expertManagement) {
        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        expertManagement.setProvinceId(sUser.getProvinceId());
        Map<String, Object> map = expertManagementRepository.getAll(curPage, pageSize, expertManagement);
        return map;
    }

    @Override
    public void delete(List<String> ids) {
        expertManagementRepository.delete(ids);
    }
}
